package com.example.gmwang.washapplication;

import android.app.Activity;
import android.app.DatePickerDialog;
import android.content.Intent;
import android.os.Bundle;
import android.support.constraint.ConstraintLayout;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.DatePicker;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import com.alibaba.fastjson.JSONObject;
import com.example.gmwang.washapplication.utils.Constants;
import com.zhy.http.okhttp.OkHttpUtils;
import com.zhy.http.okhttp.callback.StringCallback;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;

import okhttp3.Call;

/**
 * Created by gmwang on 2017/11/24.
 */

public class WashViewListActivity extends Activity {
    ConstraintLayout rlView;
    private TextView tvShowDialog;
    private Calendar cal;
    private int year, month, day;
    private String dateString = "0000";
    private List<WashRecord.DataBean> arrayList = new ArrayList<>();
    private MyProbAdapter myProbAdapter;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_washlist);
        ListView lvList = findViewById(R.id.lv_list);
        myProbAdapter = new MyProbAdapter();
        lvList.setAdapter(myProbAdapter);
        String url = Constants.url+"recordList";
        OkHttpUtils.get().url(url).addParams("cuuid","66666").build().execute(new StringCallback() {
            @Override
            public void onError(Call call, Exception e, int id) {
                Log.e("onError:", e.toString());
            }

            @Override
            public void onResponse(String response, int id) {
                Log.e("onResponse:", response);
                WashRecord washRecord = JSONObject.parseObject(response, WashRecord.class);
                //保存成功
                if("100".equals(washRecord.getCode())){
                    List<WashRecord.DataBean> list = washRecord.getData();
                    arrayList.addAll(list);
                    myProbAdapter.notifyDataSetChanged();
                }else{
                    Toast.makeText(getApplicationContext(), washRecord.getMessage(), Toast.LENGTH_LONG).show();
                }
            }
        });
    }
    public class MyProbAdapter extends BaseAdapter {

        @Override
        public int getCount() {
            return arrayList.size();
        }

        @Override
        public Object getItem(int position) {
            return arrayList.get(position);
        }

        @Override
        public long getItemId(int position) {
            return position;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            ViewHolder holder = null;
            if (convertView == null) {
                holder = new ViewHolder();
                convertView = View.inflate(WashViewListActivity.this, R.layout.activity_washviewlist, null);
                holder.tv_title = (TextView) convertView.findViewById(R.id.tv_title);
                holder.tv_record_time = (TextView) convertView.findViewById(R.id.tv_record_time);
                convertView.setTag(holder);
            } else {
                holder = (ViewHolder) convertView.getTag();
            }
            holder.tv_title.setText(arrayList.get(position).getId()+"");
            holder.tv_record_time.setText(arrayList.get(position).getRecord_time()+"");
            return convertView;
        }
    }

    public static class ViewHolder {
        TextView tv_title;
        TextView tv_record_time;
    }
}
